import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../common/local_storage.dart';

class UiFontSize {
  final double title = 17;
  final double subTitle = 15;
  final double info = 13;
  final double tabbarFontSize = 15;
  UiFontSize();
}

/// UI主题颜色
class UiTheme {
  final BuildContext context;
  const UiTheme(this.context);
  static UiTheme of(BuildContext context) => UiTheme(context);

  static ThemeMode get mode {
    var mode = LocalStorage.instance.getItem<int>('theme.mode') ?? 0;
    if (mode == 1) {
      return ThemeMode.light;
    } else if (mode == 2) {
      return ThemeMode.dark;
    } else {
      return ThemeMode.system;
    }
  }

  static toggle() {
    bool isDark = Get.theme.brightness == Brightness.dark;
    Get.changeThemeMode(isDark ? ThemeMode.light : ThemeMode.dark);
    LocalStorage.instance.setItem('theme.mode', isDark ? 1 : 2);
  }

  static String get modeText {
    Map<ThemeMode, String> map = {
      ThemeMode.system: 'Follow system',
      ThemeMode.dark: 'Enabled',
    };
    return map[mode] ?? '';
  }

  static const String fontFamily = 'NotoSansSC';

  static UiFontSize fontSize = UiFontSize();

  /// 透明度
  static const double opacity = 0.7;

  /// 组件圆角
  static const double boxRadius = 5;
  static const double boxRadiusX = 12;

  ///按钮高度
  static const double buttonHeight = 48;

  ///app 导航栏标题大小
  static const double appBarTitleFontSize = 18;

  /// 间隔 12
  static const double spacing = 12;

  /// 间隔 15
  static const double spacingX = 15;

  /// 间隔 20
  static const double spacingXX = 18;

  /// AppBar高度
  static const double toolbarHeight = 45;

  static const double toolbarSpacing = 10;

  /// 深蓝黑黑
  static const Color deepBlackBlue = Color.fromARGB(255, 21, 23, 35);
  static const Color deepBlackGrey = Color.fromARGB(255, 20, 20, 20);
  static const Color blackGrey = Color.fromARGB(255, 26, 26, 26);
  static const Color accent = Colors.blueAccent;
  static const Color primary = Color.fromARGB(255, 1, 131, 253);
  static const Color background = Color.fromARGB(255, 6, 17, 55);

  /// 浅灰色，一般用于背景色
  static const Color lightGrey = Color.fromARGB(255, 250, 250, 250);

  /// 浅色主题
  static Map<String, Color> lightColors = {
    "lightColor": Colors.white,
    'darkColor': Colors.black,
    "normalColor": Colors.black87,
    "primaryColor": primary,
    "dangerColor": Colors.redAccent,
    "infoColor": Colors.black54,
    "greyColor": Colors.grey,
    "scaffoldBackgroundColor": Colors.white,
    "scaffoldBackgroundColorBlue": lightGrey,
    "scaffoldBackgroundColorGrey": primary,
    "scaffoldBackgroundColorNormal": const Color.fromRGBO(246, 248, 255, 1),

    //列表
    "listCellBackgroundColor": Colors.transparent,
    "listCellForegroundColor": Colors.black,
    "listCellArrowColor": Colors.black54,
    //底部导航
    "bottomNavBarBackgroundColor": Colors.white,
    "bottomNavBarForegroundColor": Colors.black87,
    "linkColor": const Color.fromARGB(255, 0, 31, 82),
  };

  /// 深色主题
  static Map<String, Color> darkColors = {
    "lightColor": Colors.black,
    "darkColor": Colors.white,
    "normalColor": Colors.white70,
    "scaffoldBackgroundColor": deepBlackBlue,
    "scaffoldBackgroundColorGrey": deepBlackBlue,
    "scaffoldBackgroundColorBlue": deepBlackBlue,
    "scaffoldBackgroundColorNormal": deepBlackBlue,
    "primaryColor": Colors.white70,
    "infoColor": Colors.white70,
    "greyColor": Colors.white60,
    //列表
    "listCellBackgroundColor": const Color.fromARGB(255, 28, 30, 42),
    "listCellForegroundColor": Colors.white70,
    "listCellArrowColor": Colors.white70,
    //底部导航
    "bottomNavBarBackgroundColor": deepBlackGrey,
    "bottomNavBarForegroundColor": Colors.white70,
    "linkColor": Colors.white70,
  };

  /// 是否白天模式
  bool get isLight {
    return Theme.of(context).brightness == Brightness.light;
  }

  match({dynamic light, dynamic dark}) {
    return isLight ? light : dark;
  }

  Color matchString(name) {
    return (isLight ? lightColors[name] : darkColors[name]) ?? Colors.white;
  }

  /// 自动匹配颜色
  Color matchColor(Color? lightColor, Color? darkColor) {
    return isLight ? lightColor! : darkColor!;
  }

  /// 主题颜色
  Color get primaryColor => matchString('primaryColor');
  Color get normalColor => matchString('normalColor');
  Color get dangerColor => matchString('dangerColor');
  Color get infoColor => matchString('infoColor');

  Color get darkColor => matchString('darkColor');
  Color get lightColor => matchString('lightColor');
  Color get greyColor => matchString('greyColor');

  /// 页面背景
  Color get scaffoldBackgroundColor => matchString('scaffoldBackgroundColor');
  Color get scaffoldBackgroundColorGrey =>
      matchString('scaffoldBackgroundColorGrey');
  Color get scaffoldBackgroundColorBlue =>
      matchString('scaffoldBackgroundColorBlue');

  Color get scaffoldBackgroundColorNormal =>
      matchString('scaffoldBackgroundColorNormal');

  /// 列表
  Color get listCellBackgroundColor => matchString('listCellBackgroundColor');
  Color get listCellForegroundColor => matchString('listCellForegroundColor');
  Color get listCellArrowColor => matchString('listCellArrowColor');

  Color? get bottomSheetBackgroundColor => match(
        light: Colors.white,
        dark: background,
      );

  /// 底部导航
  Color get bottomNavBarBackgroundColor =>
      matchString('bottomNavBarBackgroundColor');
  Color get bottomNavBarForegroundColor =>
      matchString('bottomNavBarForegroundColor');

  Color get linkColor => matchString('linkColor');

  /// 面板配置
  /// 背景颜色
  Color get panelBackground {
    return match(
      light: Colors.white,
      dark: Colors.grey.withOpacity(0.08),
    );
  }

  /// 边框
  Border get panelBorder {
    return match(
      light: Border.all(width: 0.3, color: Colors.grey.withOpacity(0.2)),
      dark: Border.all(width: 0.5, color: Colors.grey.withOpacity(0.2)),
    );
  }
}
